package com.example.controller;

import com.example.common.api.ApiResult;
import com.example.dto.CaptchaDTO;
import com.example.service.CaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/auth")
public class AuthController {
    
    @Autowired
    private CaptchaService captchaService;
    
    /**
     * 获取验证码
     */
    @GetMapping("/captcha")
    public ApiResult<Map<String, String>> getCaptcha() {
        try {
            CaptchaDTO captchaDTO = captchaService.generateCaptcha();
            
            Map<String, String> captchaMap = new HashMap<>();
            captchaMap.put("captchaId", captchaDTO.getCaptchaId());
            captchaMap.put("captchaImg", captchaDTO.getCaptchaImg());
            
            return ApiResult.success(captchaMap);
        } catch (Exception e) {
            log.error("生成验证码失败", e);
            return ApiResult.failed("生成验证码失败");
        }
    }
}